package com.yndsoft.jc.ui.memberpage.setting;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yndsoft.jc.R;
import com.yndsoft.jc.activity.BaseActivity;
import com.yndsoft.jc.model.Config;
import com.yndsoft.jc.model.Product;
import com.yndsoft.jc.model.ReturnMsg;
import com.yndsoft.jc.ui.net.util.ProgressSubscriber;
import com.yndsoft.jc.utils.ConstantValues;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.lang.reflect.Type;

import butterknife.Bind;

/**
 * Created by Cool on 2017/12/26.
 */

public class AboutUsActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.base_backBtn)
    RelativeLayout base_backBtn;
    @Bind(R.id.base_title)
    TextView base_title;
    @Bind(R.id.config_content)
    WebView config_content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);

        setViews();
        setListener();
    }

    private void setViews() {
        Intent intent = getIntent();
        mHttpUtil.getConfiguration(new ProgressSubscriber<ReturnMsg>(mContext) {
            @Override
            public void next(ReturnMsg returnMsg) {
                switch (returnMsg.getCode()) {
                    case 0:
                        break;
                    case 1:
                        Type type = new TypeToken<Config>() {
                        }.getType();
                        Object fromJson = new Gson().fromJson(returnMsg.getInfo(), type);
                        Config config = (Config) fromJson;
                        base_title.setText(config.getName());

                        config_content.loadData(getNewContent(config.getNote()),
                                "text/html; charset=UTF-8", null);
                        break;
                }
            }
        }, "config", intent.getIntExtra(ConstantValues.ABOUT_AD, 10036));
    }

    /**
     * 将html文本内容中包含img标签的图片，宽度变为屏幕宽度，高度根据宽度比例自适应
     **/
    public static String getNewContent(String htmltext) {
        try {
            Document doc = Jsoup.parse(htmltext);
            Elements elements = doc.getElementsByTag("img");
            for (Element element : elements) {
                element.attr("width", "100%").attr("height", "auto");
            }

            return doc.toString();
        } catch (Exception e) {
            return htmltext;
        }
    }

    private void setListener() {
        base_backBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.base_backBtn:
                finish();
                break;
        }
    }
}
